import path from 'path';
import { fileToJsonSync, getLanguage } from './utils';

export class language {
    private readonly enJson;
    private readonly ruJson;
    private readonly zhsJson;
    private readonly zhtJson;
    private static _instance: language;
    static newInstance(extensionPath: string) {
        if (!this._instance) {
            this._instance = new language(extensionPath);
        }
        return this._instance;
    }

    static get instance() {
        return this._instance;
    }

    constructor(extensionPath: string) {
        const root = path.join(extensionPath, 'res', 'lang'),
            zhs = path.join(root, 'zhs.json'), en = path.join(root, 'en.json'),
            zht = path.join(root, 'zht.json'), ru = path.join(root, 'ru.json');
        this.zhsJson = fileToJsonSync(zhs);
        this.zhtJson = fileToJsonSync(zht);
        this.enJson = fileToJsonSync(en);
        this.ruJson = fileToJsonSync(ru);
    }

    getContent(key: string): string {
        const lang = getLanguage();
        if (lang === 'zhs') {
            if (this.zhsJson.hasOwnProperty(key)) {
                return this.zhsJson[key];
            }
        } else if (lang === 'zht') {
            if (this.zhtJson.hasOwnProperty(key)) {
                return this.zhtJson[key];
            }
        } else if (lang === 'ru') {
            if (this.ruJson.hasOwnProperty(key)) {
                return this.ruJson[key];
            }
        } else {
            if (this.enJson.hasOwnProperty(key)) {
                return this.enJson[key];
            }
        }
        return '';
    }
}